package nxu.it.movieapi.controller;

import jakarta.servlet.http.HttpServletRequest;
import nxu.it.api.common.result.ApiResult;
import nxu.it.api.model.Comment;
import nxu.it.api.model.User;
import nxu.it.movieapi.enums.ResponseType;
import nxu.it.movieapi.service.MovieCommentService;
import nxu.it.movieapi.service.UserLoginService;
import nxu.it.movieapi.service.UserRegisterService;
import nxu.it.movieapi.service.UserCommentService;
import nxu.it.movieapi.util.ApiResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/movie/api/v2")
public class MovieApiV2Controller {

    @Autowired
    private UserRegisterService userRegisterService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private MovieCommentService movieCommentService;
    @Autowired
    private UserCommentService userCommentService;

    @PostMapping("/register")
    public ResponseEntity<ApiResult<User>> register(@RequestBody User user) {
        try {
            User registeredUser = userRegisterService.register(user);
            ApiResult<User> apiResult = ApiResult.success(registeredUser);
            // 使用 HttpStatus.CREATED 获取枚举值
            return ResponseEntity.status(HttpStatus.CREATED).body(apiResult);
        } catch (IllegalArgumentException e) {
            ApiResult<User> apiResult = ApiResultUtils.failure(e.getMessage());
            // 使用 HttpStatus.BAD_REQUEST 获取枚举值
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(apiResult);
        }
    }

    @PostMapping("/login")
    public ResponseEntity<ApiResult<User>> login(@RequestBody Map<String, String> credentials) {
        String username = credentials.get("username");
        String password = credentials.get("password");
        ApiResult<User> apiResult = userLoginService.login(username, password);
        if (apiResult.isSuccess()) {
            return ResponseEntity.ok(apiResult);
        } else {
            return ResponseEntity.status(ResponseType.FAILURE.getCode()).body(apiResult);
        }
    }

    @PostMapping("/movies/{movieId}/comments")
    public ResponseEntity<ApiResult<Comment>> commentOnMovie(
            @PathVariable String movieId,
            @RequestBody CommentRequest commentRequest,
            HttpServletRequest request) {

        // 从请求头中获取用户ID，实际应用中应从认证信息中获取
        String userId = request.getHeader("X-User-Id");

        Comment comment = movieCommentService.commentOnMovie(
                movieId,
                commentRequest.getContent(),
                commentRequest.getRating(),
                userId);

        ApiResult<Comment> apiResult = ApiResult.success(comment);
        return ResponseEntity.ok(apiResult);
    }

    // 请求体内部类
    public static class CommentRequest {
        private String content;
        private Integer rating;

        // Getters and Setters
        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Integer getRating() {
            return rating;
        }

        public void setRating(Integer rating) {
            this.rating = rating;
        }
    }

    @GetMapping("/users/{userId}/comments")
    public ResponseEntity<ApiResult<List<Comment>>> getCommentsByUserId(
            @PathVariable String userId,
            @RequestHeader(value = "Authorization", required = false) String authorizationHeader) {

        // 在实际应用中，这里应该有权限验证逻辑
        // 例如，验证 authorizationHeader 是否有效

        List<Comment> comments = userCommentService.getCommentsByUserId(userId);
        if (comments.isEmpty()) {
            ApiResult<List<Comment>> apiResult = ApiResultUtils.failure("用户评论不存在或无权限查看");
            return ResponseEntity.status(404).body(apiResult);
        } else {
            ApiResult<List<Comment>> apiResult = ApiResult.success(comments);
            return ResponseEntity.ok(apiResult);
        }
    }
}
